//******************************************************************************
//this code is protected by the GNU affero GPLv3
//author:Sylvain BERTRAND <sylvain.bertrand AT gmail dot com>
//                        <digital.ragnarok AT gmail dot com>
//******************************************************************************

//------------------------------------------------------------------------------
//compiler stuff
//------------------------------------------------------------------------------
#include <stdarg.h>
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//ulinux stuff
//------------------------------------------------------------------------------
#include <ulinux/compiler_types.h>
#include <ulinux/types.h>
#include <ulinux/sysc.h>
#include <ulinux/error.h>

#include <ulinux/utils/ascii/ascii.h>
#include <ulinux/utils/ascii/string/string.h>
#include <ulinux/utils/ascii/string/vsprintf.h>
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//protocol
//------------------------------------------------------------------------------
#include <wayland/display.h>
#include <wayland/callback.h>
#include <wayland/compositor.h>
#include <wayland/registry.h>
#include <wayland/shm.h>
#include <wayland/shm_pool.h>
#include <wayland/buffer.h>
#include <wayland/seat.h>
#include <wayland/output.h>
#include <wayland/keyboard.h>
#include <wayland/pointer.h>
#include <wayland/wayland.h>
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//our private stuff
//------------------------------------------------------------------------------
#include "globals.h"
#include "wire/wl_callback.h"
#include "callbacks.h"
//------------------------------------------------------------------------------
static void ev_done(k_u32 *ev,void (*done)(k_u32 serial))
{
  k_u16 dws=(ev[1]>>16)>>2;
  if(dws<3){
    PERRC("FATAL:callback::done event, missing serial\n");
    sysc(exit_group,1,-1);
  }
  if(done) done(ev[3]);
}

void wl_callback_ev(k_u32 *ev,void *listener)
{
  if(!listener) return;

  k_u16 dws=(ev[1]>>16)>>2;

  if(dws<2){
    PERRC("FATAL:callback event, missing opcode\n");
    sysc(exit_group,1,-1);
  }

  k_u16 op=ev[1]&0xffff;

  struct wl_callback_listener *callback_listener=listener;
  switch(op){
  case WL_CALLBACK_DONE:
    ev_done(ev,callback_listener->done);
    break;
  }
}
